﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General.MVP;
using ESF.SSO.Core.BusinessLogic;
using ESF.General.Entity;
using ESF.SSO.Core.Entities.BasicReferences;
using ESF.SSO.Core.Entities;
using ESF.General;

#endregion


namespace ESF.SSO.Presenter.Admin.User
{
	public class EditUserPresenter : BaseEditPresenter<IEditUserViewer, ESF.SSO.Core.Entities.User>
	{
		#region Constructor
		
		public EditUserPresenter (IEditUserViewer pViewer) : base (pViewer) {}
		
		#endregion
				
		
		#region Funciones

		public override void PrepareFields()
		{
		}


		public override ESF.SSO.Core.Entities.User Get()
		{
			return CtrlUser.Get (new ESF.SSO.Core.Entities.User () { ID = _Viewer.ItemID });
		}
		
		
		public override void Load (ESF.SSO.Core.Entities.User pUser)
		{
			try
			{
				_Viewer.UserAlias = pUser.Alias;
				_Viewer.UserBirthDate = pUser.BirthDate;
				_Viewer.UserEmail = pUser.Email;
				
				if (pUser.Enterprise != null)
				{
					_Viewer.UserEnterprise = pUser.Enterprise.Name;
				}
				
				_Viewer.UserFirstName = pUser.FirstName;
				_Viewer.UserSecondName = pUser.SecondName;
				_Viewer.UserSurName = pUser.SurName;
				_Viewer.UserLastName = pUser.LastName;
				
				_Viewer.UserIdentificationNumber = pUser.IdentificationNumber;
				_Viewer.UserIdentificationTypeID = pUser.IdentificationType.ID.Value;
				
				_Viewer.UserIsEnabled = pUser.IsEnabled;
				
				_Viewer.UserCreationAlias = pUser.UserCreationAlias;
				_Viewer.CreationDateTime = pUser.CreationDateTime;
				_Viewer.UserLastUpdateAlias = pUser.UserLastUpdateAlias;
				_Viewer.LastUpdateDateTime = pUser.LastUpdateDateTime;
				
				_Viewer.UserGenderID = (pUser.Gender == null ? null : pUser.Gender.ID);
				
				_Viewer.UserSecurityQuestionID = pUser.SecurityQuestion.ID.Value;
				_Viewer.UserSecurityQuestionAnswer = pUser.SecurityQuestionAnswer;
				
				_Viewer.UserProfession = pUser.Profession;
				_Viewer.UserPosition = pUser.Position;

				_Viewer.UserGroups = pUser.UserGroups;

				_Viewer.UserRights = pUser.UserRights;
				
				// TODO: Editar multiples localizaciones
				if (!DataHelper.IsEmpty (pUser.Locations))
				{
					_Viewer.Location = pUser.Locations [0];
				}
				
				_Viewer.IsEnabledResetPassword = true;
				
				_Viewer.ItemID = pUser.ID;

				LoadSteps ();
			}
			catch (Exception pEx)
			{
				LogError (pEx);
			}
		}
		
		
		public void LoadSteps ()
		{
			_Viewer.ExtraAppSteps = CtrlBasicReference.List (BasicReferenceTypes.Application).Where (a => !string.IsNullOrEmpty (a.Extra_II)).ToList ();
		}
		
		
		public override void Clear ()
		{
			base.Clear ();

			ClearError ();
			
			_Viewer.Location = null;
			_Viewer.UserAlias = string.Empty;
			_Viewer.UserBirthDate = null;
			_Viewer.UserEmail = string.Empty;
			_Viewer.UserEnterprise = string.Empty;
			_Viewer.UserFirstName = string.Empty;
			_Viewer.UserGenderID = null;
			_Viewer.UserIdentificationNumber = string.Empty;
			_Viewer.UserIdentificationTypeID = null;
			_Viewer.UserLastName = string.Empty;
			_Viewer.UserSecondName = string.Empty;
			_Viewer.UserSecurityQuestionAnswer = string.Empty;
			_Viewer.UserSecurityQuestionID = null;
			_Viewer.UserSurName = string.Empty;
			_Viewer.UserIsEnabled = null;
			_Viewer.UserPosition = string.Empty;
			_Viewer.UserProfession = string.Empty;
			_Viewer.Location = null;

			_Viewer.NotifyUserEmail = true;
			
			_Viewer.LastUpdateDateTime = null;
			_Viewer.CreationDateTime = null;
			
			_Viewer.IsEnabledResetPassword = false;
			_Viewer.UserGroups = null;
			_Viewer.UserRights = CtrlUser.GetUserBasicReferenceValues (new ESF.SSO.Core.Entities.User (), BasicReferenceTypes.UserRights).ToList ();

			LoadSteps ();
		}


		public bool SetNewPassword ()
		{
			try
			{
				CtrlUser.SetNewPassword (_Viewer.UserAlias, _Viewer.UserNewPassword);

				_Viewer.frmInfoMsg = "La Contraseña del Usuario ha sido cambiada";

				_Viewer.UserNewPassword = string.Empty;

				return true;
			}
			catch (Exception pEx)
			{
				LogError (pEx);

				return false;
			}
		}


		public override void Save()
		{
			ESF.SSO.Core.Entities.User user;
		
		
			user = new ESF.SSO.Core.Entities.User ()
			{
				ID = _Viewer.ItemID,
				Alias = _Viewer.UserAlias,
				BirthDate = _Viewer.UserBirthDate,
				Email = _Viewer.UserEmail,
				Enterprise = new ESF.SSO.Core.Entities.Enterprise () { Name = _Viewer.UserEnterprise },
				FirstName = _Viewer.UserFirstName,
				Gender = new ESF.General.Entity.BasicReference () { ID = _Viewer.UserGenderID },
				IdentificationNumber = _Viewer.UserIdentificationNumber,
				IdentificationType = new ESF.General.Entity.BasicReference () { ID = _Viewer.UserIdentificationTypeID },
				LastName = _Viewer.UserLastName,
				SecondName = _Viewer.UserSecondName,
				SecurityQuestion = new ESF.General.Entity.BasicReference () { ID = _Viewer.UserSecurityQuestionID },
				SecurityQuestionAnswer = _Viewer.UserSecurityQuestionAnswer,
				SurName = _Viewer.UserSurName,
				IsEnabled = _Viewer.UserIsEnabled,
				LastUpdateDateTime = DateTime.Now,
				UserGroups = _Viewer.UserGroups,
				NotifyUserEmail = _Viewer.NotifyUserEmail,
				UserRights = _Viewer.UserRights,
				Profession = _Viewer.UserProfession,
				Position = _Viewer.UserPosition,

				Locations = new List<ESF.SSO.Core.Entities.Location> 
				{
					_Viewer.Location
				}
			};
		
			try
			{
				CtrlUser.SignUp (user);
				
				_Viewer.frmInfoMsg = "Los datos de la cuenta han sido registrados";
				
				_Viewer.ItemID = user.ID;
				
				_Viewer.CreationDateTime = user.CreationDateTime;
				_Viewer.LastUpdateDateTime = user.LastUpdateDateTime;
			}
			catch (Exception pEx)
			{
				LogError (pEx);
			}
		}


		public override void Delete()
		{
			throw new NotImplementedException();
		}
		
		
		public void AutoResetPassword ()
		{
			ESF.SSO.Core.Entities.User user;
			
			
			if (_Viewer.ItemID == null)
			{
				_Viewer.frmErrorMsg = "Solo se puede luego que el usuario ha sido registrado";
				return;
			}
		
		
			user = new ESF.SSO.Core.Entities.User ()
			{
				ID = _Viewer.ItemID,
				Alias = _Viewer.UserAlias,
				UserLastUpdateAlias = _Viewer.frmUserAlias
			};
			
			try
			{
				CtrlUser.AutoResetPassword (user);
			
				_Viewer.frmInfoMsg = "La contraseña ha sido asignada automaticamente\ny se ha notificado al usuario mediante su correo electrónico";
			}
			catch (Exception pEx)
			{
				LogError (pEx);
			}
		}
		
		#endregion
	}
}
